#include "../../../Include/DataType/Graph/WizDataTypeGraph.hpp"
#include "../../../Include/DataType/Graph/WizDataTypeGraphNode.hpp"
#include "../../../Include/DataType/Graph/WizDataTypeGraphLink.hpp"


namespace Wiz
{
    namespace DGraph
    {
        ////////////////////////////////////////////////////////////////////
        ///==============================================================///
        Type::~Type()
        {
            this->Clear();
        }

        ::Wiz::Void::Type Type::Clear()
        {
            ::Wiz::DGraph::Node::Set::Iterator   Curr;
            ::Wiz::DGraph::Node::Set::ForEach    FE(Curr, this->m_Nodes);

            while (FE.HasMoreElement())
            {
                this->DestroyNode(*Curr);
            }

            this->m_Nodes.Clear();
        }

        ::Wiz::Void::Type Type::InsertNode(::Wiz::DGraph::Node::Ptr NodePtr)
        {
            this->m_Nodes.Insert(NodePtr);
        }

        ::Wiz::DGraph::Node::Ptr Type::CreateNode()
        {
            return new ::Wiz::DGraph::Node::Type();
        }
        ::Wiz::Void::Type Type::DestroyNode(::Wiz::DGraph::Node::Ptr NodePtr)
        {
            delete NodePtr;
        }
        ///==============================================================///
        ////////////////////////////////////////////////////////////////////

        ////////////////////////////////////////////////////////////////////
        ///==============================================================///
        ::Wiz::Bool::Type IsRingStruct(::Wiz::DGraph::Node::Ptr From, ::Wiz::DGraph::Node::Ptr To)
        {
            if (From == To)
            {
                return ::Wiz::Bool::True;
            }

            ::Wiz::DGraph::Link::Output::Set::ConstIterator   OCurr;
            ::Wiz::DGraph::Link::Output::Set::ConstForEach    OFE(OCurr, To->GetOutputSet());

            while (OFE.HasMoreElement())
            {
                ::Wiz::DGraph::Link::Set::ConstIterator Curr;
                ::Wiz::DGraph::Link::Set::ConstForEach  FE(Curr, (*OCurr)->GetLinkSet());
                while (FE.HasMoreElement())
                {
                    if (IsRingStruct(From, (*Curr)->GetOwner()))
                    {
                        return ::Wiz::Bool::True;
                    }
                }
            }
            return ::Wiz::Bool::False;
        }
        ///==============================================================///
        ////////////////////////////////////////////////////////////////////
    } /// end of namespace DGraph
    ///==============================================================///
    ////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////
    ///==============================================================///
    namespace UGraph
    {
        Type::~Type()
        {
            this->Clear();
        }

        ::Wiz::Void::Type Type::Clear()
        {
            ::Wiz::UGraph::Node::Set::Iterator   Curr;
            ::Wiz::UGraph::Node::Set::ForEach    FE(Curr, this->m_Nodes);

            while (FE.HasMoreElement())
            {
                this->DestroyNode(*Curr);
            }

            this->m_Nodes.Clear();
        }
        ::Wiz::Void::Type Type::InsertNode(::Wiz::UGraph::Node::Ptr NodePtr)
        {
            this->m_Nodes.Insert(NodePtr);
        }
        ::Wiz::UGraph::Node::Ptr Type::CreateNode()
        {
            return new ::Wiz::UGraph::Node::Type();
        }
        ::Wiz::Void::Type Type::DestroyNode(::Wiz::UGraph::Node::Ptr NodePtr)
        {
            delete NodePtr;
        }
    } /// end of namespace UGraph
    ///==============================================================///
    ////////////////////////////////////////////////////////////////////
} /// end of namespace Wiz


